package com.nmbaiyun.yangrong.uhfg;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import java.util.Set;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class UHFGUtilModule extends UniModule {
    String TAG = "UHFGUnitModule";
    public static int REQUEST_CODE = 1000;
//    计数中
    boolean isCounting = false;


    @UniJSMethod(uiThread = false)
    public void getSyncRFID(UniJSCallback callback) {

        if(callback != null) {
            String rfid = "";
            JSONObject data = new JSONObject();
            try {
                UHFGUtil until = UHFGUtil.getInstance();

                rfid = until.getSyncRFID();
                data.put("code", "200");
                data.put("data",rfid);
                callback.invoke(data);
            }catch (Exception e){
                data.put("code", "400");
                data.put("msg","异常"+e.getMessage());
                callback.invoke(data);
            }

        }else {
            Log.i(TAG,"未传回调函数");
        }
    }

    @UniJSMethod(uiThread = false)
    public void getSyncRFIDList(UniJSCallback callback) {

        if(callback != null) {

            JSONObject data = new JSONObject();
            try {
                UHFGUtil until = UHFGUtil.getInstance();

                Set<String> list = until.getSyncRFIDList();
                data.put("code", "200");
                data.put("data",list);
                callback.invoke(data);
            }catch (Exception e){
                data.put("code", "400");
                data.put("msg","异常"+e.getMessage());
                callback.invoke(data);
            }

        }else {
            Log.i(TAG,"未传回调函数");
        }
    }

    @UniJSMethod(uiThread = false)
    public void asyncCount(int delys,UniJSCallback callback){
        if(callback != null) {
            String rfid = "";
            JSONObject data = new JSONObject();
            try {
                if(isCounting) {
                    return;
                }
                UHFGUtil until = UHFGUtil.getInstance();
                if (delys == 0){
                    delys = 500;
                }
                until.count(new UHFGUtilListenner() {
                    @Override
                    public void callBackResult(int num, Set<String> list) {
                        Log.i(TAG+"toGetAllDevices",num+""+list.toString());
                        isCounting = false;
                        data.put("code", "200");
                        data.put("msg","获取数据成功");
                        data.put("total",num);
                        data.put("data",list);
                        callback.invoke(data);
                    }
                },delys);
            }catch (Exception e){
                data.put("code", "400");
                data.put("msg","异常"+e.getMessage());
                callback.invoke(data);
            }

        }else {
            Log.i(TAG,"未传回调函数");
        }
    }



    @UniJSMethod(uiThread = false)
    public JSONObject getPower(){
        UHFGUtil until = UHFGUtil.getInstance();
        String res = until.getPower();
        JSONObject data = new JSONObject();
        data.put("code", "200");
        data.put("data",res);
        return data;
    }



}
